﻿namespace Useful.MVP2
{
    public abstract class ComplexListPresenter<ListT, ItemT, DetailView> : BaseListPresenterT<ListT, ItemT, BaseListModelT<ListT>, IRepository<ListT, ItemT>, IListViewT<ListT, ItemT>>
        where ItemT : class, new()
        where ListT: class
        where DetailView : IComplexDetailView<ListT, ItemT>, new()
    {
        protected ComplexListPresenter(IListViewT<ListT, ItemT> view, IRepository<ListT, ItemT> repository, IPresenterImpl presenterImpl)
            : base(view, repository, presenterImpl)
        {
        }

        //protected override void OnViewInited(InitListViewEventArgs e)
        //{
        //    base.OnViewInited(e);
        //}

        protected virtual ListT LoadData()
        {
            ISelectParams selectParams = CreateSelectParams();

            return Repository.Select(selectParams);
        }

        protected abstract ISelectParams CreateSelectParams();

        protected override void OnRefreshClicked()
        {
            ListT list = null;

            ShowWaiting(Repository, "Загрузка", () =>
            {
                list = LoadData();
            });

            Model.ListDataSource = list;
            View.ListDataSourceT = Model.ListDataSource;
        }

        protected override void OnAddClicked()
        {
            IComplexDetailView<ListT, ItemT> form = new DetailView();

            form.Init(Repository, Model.AllowAdd | Model.AllowEdit, null);

            form.ShowDialog();
        }

        protected override void OnEditClicked(ItemT item)
        {
            IComplexDetailView<ListT, ItemT> form = new DetailView();

            form.Init(Repository, Model.AllowAdd | Model.AllowEdit, item);

            form.ShowDialog();
        }

        protected override void OnDeleteClicked(ItemT item)
        {
            ShowWaiting(Repository, "Удаление", () =>
            {
                Repository.Delete(item);
            });
        }
    }
}
